package com.lethe.medical.dao;

import com.lethe.medical.base.BaseDao;
import com.lethe.medical.entity.UserEntity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDao extends BaseDao {
    /**
     * 根据用户名查找用户是否存在
     * @param username
     * @return
     */
    public UserEntity findUserByUserName(String username) {
        UserEntity userEntity = null;
        String sql = "select * from md_user where username = ?";
        ResultSet rs = super.query(sql, username);
        try {
            while(rs.next()) {
                userEntity = new UserEntity();
                userEntity.setId(rs.getInt("id"));
                userEntity.setUsername(rs.getString("username"));
                userEntity.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userEntity;

    }

    /*public UserEntity login(UserEntity user) {
        UserEntity u = null;
        String sql = "select * from md_user where username = ? and password = ?";
        ResultSet rs = super.query(sql, user.getUsername(),user.getPassword());
        try {
            while(rs.next()) {
                u = new UserEntity();
                u.setId(rs.getInt("id"));

                u.setUsername(rs.getString("username"));
                u.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return u;

    }*/

    public UserEntity login(UserEntity userEntity){
        UserEntity u = null;
        String sql = "select * from md_user where username = ? and password = ?";

        //生成参数的数组
        Object[] users = new Object[2];
        users[0] = userEntity.getUsername();
        users[1] = userEntity.getPassword();
        List<UserEntity> list = super.query(sql, users , UserEntity.class);

        if(list.size()>0){
            u = list.get(0);
        }

        return u;

    }




}
